﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using PersonHealth.ViewModel;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using Telerik.Windows.Controls;
using Core.Model;
using System.Windows.Threading;
using System.Collections;

namespace PersonHealth.View
{
    /// <summary>
    /// PersonInfoPage.xaml 的交互逻辑
    /// </summary>
    public partial class PersonInfoPage : Page
    {
        MeasureUserViewModel userModel = new MeasureUserViewModel();
        public PersonInfoPage()
        {
            InitializeComponent();

            //IDepartmentService dser = new DepartmentService();
            //var root = dser.Get(1);
            //List<Department> list = new List<Department>();
            //list.Add(root);
            //this.tvDepartment.ItemsSource = list;
            //this.tvDepartment.LoadOnDemand += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(treeView_LoadOnDemand);
            //this.tvDepartment.ItemPrepared += new EventHandler<RadTreeViewItemPreparedEventArgs>(treeView_ItemPrepared);

            User user = AppData.User;
            userModel.Id = user.Id;
            userModel.UserName = user.UserName;
            userModel.IdCard = user.IdCard;
            userModel.SexId = user.SexId;
            userModel.UserImageUrl = user.UserImageUrl;
            userModel.Height = user.Height;
            userModel.Weight = user.Weight;
            userModel.Mobile = user.Mobile;
            userModel.TelPhone = user.TelPhone;
            userModel.Birthday = user.Birthday;
            userModel.Address = user.Address;
            userModel.PostCode = user.PostCode;
            userModel.Email = user.Email;
            userModel.DepartmentId = user.DepartmentId;
            userModel.Department = user.Description;

            this.DataContext = userModel;
        }

        public PersonInfoPage(Department obj)
        {
            InitializeComponent();
            User user = AppData.User;
            userModel.Id = user.Id;
            userModel.UserName = user.UserName;
            userModel.IdCard = user.IdCard;
            userModel.SexId = user.SexId;
            userModel.UserImageUrl = user.UserImageUrl;
            userModel.Height = user.Height;
            userModel.Weight = user.Weight;
            userModel.Mobile = user.Mobile;
            userModel.TelPhone = user.TelPhone;
            userModel.Birthday = user.Birthday;
            userModel.Address = user.Address;
            userModel.PostCode = user.PostCode;
            userModel.Email = user.Email;
            userModel.DepartmentId = user.DepartmentId;
            userModel.Department = obj.Name;
            this.DataContext = userModel;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ScreenKeyboard.ShowScreenKeyboard();
        }

        //save
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IUserService ser = new UserService();
            User user = ser.Get(userModel.Id);
            user.Id = userModel.Id;
            user.UserName = userModel.UserName;
            user.IdCard = userModel.IdCard;
            user.SexId = userModel.SexId;
            user.UserImageUrl = userModel.UserImageUrl;
            user.Height = userModel.Height;
            user.Weight = userModel.Weight;
            user.Mobile = userModel.Mobile;
            user.TelPhone = userModel.TelPhone;
            user.Birthday = userModel.Birthday;
            user.Address = userModel.Address;
            user.PostCode = userModel.PostCode;
            user.Email = userModel.Email;
            user.DepartmentId = userModel.DepartmentId;
            user.Description = userModel.Department;
            ser.Update(user);

            AppData.User = ser.Get(userModel.Id) as MeasureUser;

            NavigationService.GoBack();
        }
 
        private void tbDepartment_GotFocus(object sender, RoutedEventArgs e)
        {
            //Func<object, IEnumerable> onGetData = new Func<object, IEnumerable>(GetData);
            //Action<object> onSelected = new Action<object>(DepartmentSelected);
            //NavigationService.Content = new TreeSelectPage(new Department { Id = 1 }, onGetData, onSelected);
        }

        private IEnumerable GetData(object obj)
        {
            IDepartmentService dser = new DepartmentService();
            return dser.GetChildren((obj as IItem).Id);
        }

        private void DepartmentSelected(object obj)
        {
            tbDepartment.Text = (obj as IItem).Name;
        }

       
    }
}
